package com.cbtx.tablet.dialog

import android.app.Dialog
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.getSystemService
import com.cbtx.tablet.R

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/6 4:35 PM
 * desc   :
 */
class QRCodeDialog(var mContext: Context, var qrCodeBitmap: Bitmap) : Dialog(mContext, R.style.MMTheme) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
        hideBottomButton()
    }

    private fun initView() {
        val contentView: View = LayoutInflater.from(mContext).inflate(R.layout.sp_dialog_qr_code, null)
        val imageView = contentView.findViewById<ImageView>(R.id.mIvQRCode)
        val tvVersion = contentView.findViewById<TextView>(R.id.tv_version)
        imageView.setImageBitmap(qrCodeBitmap)
        setContentView(contentView)
    }


    private fun hideBottomButton() {
        val window = window
        val params = window!!.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
    }

    fun packageName(context: Context): String? {
        val manager = context.packageManager
        var name: String? = null
        try {
            val info = manager.getPackageInfo(context.packageName, 0)
            name = info.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return name
    }

    override fun show() {
        var wm = window
        val lp = wm?.attributes
        lp?.width = 610
        lp?.height = 410
        window?.attributes = lp
        super.show()
    }
}